import { Component, ViewChild,forwardRef } from '@angular/core';
import { CTAFComponent,ComponentBase,ChangeDetectorRef,Message } from '@ctaf/framework';
import { ForeignExchangeChildComponent } from './foreign-exchange-child/foreignExchangeChild.component';
import './foreignExchange.less';
@CTAFComponent({
    selector: 'qdm-foreignExchange',
    templateUrl: 'foreignExchange.component.html'
})

export class ForeignExchangeComponent extends ComponentBase{
    @ViewChild(forwardRef(()=>ForeignExchangeChildComponent)) child_obj:ForeignExchangeChildComponent;
    private msgToChild: string;
    private msgFromChild: string;
    private source = [
        { label: 'test1', value: 'test1' },
        { label: 'test2', value: 'test2', disabled: 'true', checked: 'true' },
        { label: 'test3', value: 'test3' },
        { label: 'test4', value: 'test4' }
    ];

    constructor(cdr: ChangeDetectorRef) {
        super(cdr);

        this.messageService.On('demoComponent1', (message: Message) => {
            alert("收到消息:"+message.subject);
        });

        this.messageService.On('demoComponent2', (message: Message) => {
            alert("收到消息:"+message.subject);
        });
    }

    BoradCaset() {
        this.messageService.Broadcast(new Message('main boradcaset', '...'));
    }

    ngOnInit(){
        this.msgToChild ="我是父组件发给子组件的消息";
    }    
    Data1(event, data) {
        console.log(event.value);
    }
    //从子组件接收消息
    receive(msg: string){
        this.msgFromChild = msg;
    }
    OnClick(){
      this.child_obj.fun1();//ViewChild的demo步骤4:调用子组件里的方法
    }
    //linkButton点击事件
    buttonClick(msg: string){
        alert(msg);
    }
}

@CTAFComponent({
    selector: 'ctaf-demo-mescp1',
    template: `<div><ctaf-cp-button (click)='sendMessage()' title='测试控件1发送消息'></ctaf-cp-button></div>`
})
export class MessageService1Component extends ComponentBase {
    constructor(cdr: ChangeDetectorRef) {
        super(cdr);

        this.messageService.received.subscribe((message: Message) => {
            alert('组件1收到消息');
        });
    }

    sendMessage() {
        this.messageService.Send('demoComponent1', new Message('组件1发出的消息', '...'));
    }
}

@CTAFComponent({
    selector: 'ctaf-demo-mescp2',
    template: `<div><ctaf-cp-button (click)='sendMessage()' title='测试控件2发送消息'></ctaf-cp-button></div>`
})
export class MessageService2Component extends ComponentBase {
    constructor(cdr: ChangeDetectorRef) {
        super(cdr);

        this.messageService.received.subscribe((message: Message) => {
            alert('组件2收到消息');
        });
    }
    sendMessage() {
        this.messageService.Send('demoComponent2', new Message('组件2发出的消息', '...'));
    }
}


